//
//  PosterView.m
//  卢卡的时光网
//
//  Created by 卡神 on 15/7/24.
//  Copyright © 2015年 lok. All rights reserved.
//

#import "PosterView.h"
#import "common.h"
#import "SamllPosterView.h"
#import "BigPosterView.h"

#define kHeaderViewH 136
#define kIndexCollectionViewH 100
#define kBottomTitleLableH 35
#define kBottomTitleLableY (kHeight-kTabBarHeight-kBottomTitleLableH)
#define kHeaderOffH   36
#define kPosterColletionViewY  (kHeaderViewH-kHeaderOffH)
#define kPosterColletionViewH  (kHeight-kPosterColletionViewY-kTabBarHeight-kBottomTitleLableH)
@implementation PosterView

- (nonnull instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self _createPosterView];
        [self _createLabel];
        [self _createCoverView];
        [self _createHeader];
        [self _createGesture];
    }
    return self;
}

#pragma mark -observer function 

-(void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable id)object change:(nullable NSDictionary *)change context:(nullable void *)context {
    NSInteger index = [[change valueForKey:@"new"] integerValue];
    NSIndexPath *path = [NSIndexPath indexPathForRow:index inSection:0];
    if ([object isKindOfClass:[BigPosterView class]] && _small.currentIndex != index) {
        [_small scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        _small.currentIndex = index;
    } else if([object isKindOfClass:[SamllPosterView class]] && _big.currentIndex!= index) {
        [_big scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        _big.currentIndex = index;
    }
    _label.text = [_imageData[index] name];
}

#pragma mark -functions of viewDidLoad
//创建头视图
- (void)_createHeader {
    _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, -kHeaderOffH, kWidth, kHeaderViewH)];
    UIImage *bgImag = [UIImage imageNamed:@"indexBG_home"];
    bgImag = [bgImag stretchableImageWithLeftCapWidth:0 topCapHeight:1];
    UIImageView *bg = [[UIImageView alloc] initWithFrame:_headerView.bounds];
    bg.image = bgImag;
    [_headerView addSubview:bg];
    [self _createHeaderCollectionView];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake((kWidth - 80) / 2, kHeaderViewH - 20, 80, 20)];
    button.tag = 100;
    [button addTarget:self action:@selector(changeView:) forControlEvents:UIControlEventTouchUpInside];
    [button setImage:[UIImage imageNamed:@"down_home"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"up_home"] forState:UIControlStateSelected];
    [_headerView addSubview:button];
    [self addSubview:_headerView];
    
}

//创建头视图里面的小海报视图
- (void)_createHeaderCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumLineSpacing = 20;
    CGFloat width = kIndexCollectionViewH  / 2;
    _small = [[SamllPosterView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kIndexCollectionViewH) collectionViewLayout:layout];
    _small.itemWidth = width;
    [_small addObserver:self forKeyPath:@"currentIndex" options:NSKeyValueObservingOptionNew context:nil];
    [_headerView addSubview: _small];
}

- (void)_createPosterView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = lineSpacing;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _big = [[BigPosterView alloc] initWithFrame: CGRectMake(0, kPosterColletionViewY, kWidth, kPosterColletionViewH) collectionViewLayout: layout];
    _big.itemWidth = self.width * 3 / 4;
    [_big addObserver:self forKeyPath:@"currentIndex" options:NSKeyValueObservingOptionNew context:nil];
    [self addSubview:_big];
    
}


- (void)_createLabel {
    _label = [[UILabel alloc] initWithFrame:CGRectMake(0, kBottomTitleLableY - 7, kWidth, kBottomTitleLableH - 7)];
    _label.textAlignment = NSTextAlignmentCenter;
    _label.text = @"双枪";
    _label.font = [UIFont systemFontOfSize:28 weight:10];
    _label.textColor = [UIColor whiteColor];
    [self addSubview:_label];
    
}
//创建笼罩视图
- (void)_createCoverView {
    _coverView = [[UIControl alloc] initWithFrame:self.bounds];
    _coverView.hidden = YES;
    [_coverView addTarget:self action:@selector(cover) forControlEvents:UIControlEventTouchUpInside];
    [_coverView setBackgroundColor:[UIColor colorWithWhite:0.6 alpha:0.6]];
    [self addSubview:_coverView];
}
#pragma mark -coverView's change button
- (void)changeView: (UIButton *)button {
    
    if (button.selected) {
        [self hideHeader];
    } else {
        [self showHeader];
    }
    button.selected = !button.selected;
}

- (void)cover {
    [self hideHeader];
    UIButton *button = (UIButton *)[_headerView viewWithTag:100];
    button.selected = NO;
}

#pragma mark -swipe gesture of coverView
- (void)_createGesture {
    UISwipeGestureRecognizer *swipe = [[ UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)];
    swipe.direction = UISwipeGestureRecognizerDirectionDown;
    [self addGestureRecognizer:swipe];
    
}
- (void)swipe {
    [self showHeader];
    UIButton *button = (UIButton *)[_headerView viewWithTag:100];
    button.selected = YES;
}
//笼罩视图的隐藏和显示
- (void)showHeader {
    [UIView beginAnimations:@"animation" context:nil];
    [UIView setAnimationDuration:0.5];
    _headerView.top = 64;
    _coverView.hidden = NO;
    [UIView commitAnimations];
}

- (void)hideHeader {
    [UIView beginAnimations:@"animation1" context:nil];
    [UIView setAnimationDuration:0.5];
    _headerView.top = -36;
    _coverView.hidden = YES;
    [UIView commitAnimations];
}

#pragma mark -setter
- (void)setImageData:(NSMutableArray *)imageData {
    _imageData = imageData;
    _small.imageData = imageData;
    _big.imageData = imageData;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
